package cn.kgc.smbms.controll;

import cn.kgc.smbms.pojo.Role;
import cn.kgc.smbms.pojo.User;
import cn.kgc.smbms.pojo.UserExt;
import cn.kgc.smbms.service.RoleService;
import cn.kgc.smbms.service.UserService;
import cn.kgc.smbms.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    //登陆验证
    @RequestMapping("doLogin")
    public String getLogin(@RequestParam("userCode") String userCode,
                           @RequestParam("userPassword") String userPassword,
                           HttpSession session, HttpServletRequest request) {
        User user = userService.login(userCode, userPassword);
        if (null == user) {
            session.setAttribute("errorMessege", "用户名密码错误");
            return "login";
        }
        session.setAttribute("user", user);
        return "frame";
    }

    @RequestMapping("logout")
    public String getLogout(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }

    @RequestMapping("selectAllUser")
    public String getAllUserList(Model model,
                                 @RequestParam(value = "pageIndex", required = false) String pageIndex,
                                 @RequestParam(value = "queryUserName", required = false) String queryUserName,
                                 @RequestParam(value = "queryUserRole", required = false) String queryUserRole) {
        List<User> userList = null;
        List<Role> roleList = null;
        roleList = roleService.selectRoleList();
        model.addAttribute("roleList", roleList);
        Page<User> page = new Page<>();
        int pageNo = 1;
        int pageSize = 4;
        if (pageIndex != null) {
            pageNo = Integer.valueOf(pageIndex);
        }
        int currentPageNo = (pageNo - 1) * pageSize;
        int totalRecode = userService.selectCount();
        userList = userService.selectAllUser(currentPageNo, pageSize, queryUserName, queryUserRole);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotalRecode(totalRecode);
        int totalPage = page.getTotalPage();
        if (userList != null) {
            model.addAttribute("userList", userList);
            model.addAttribute("totalRecode", totalRecode);
            model.addAttribute("pageNo", pageNo);
            model.addAttribute("totalPage", totalPage);
            //session.setAttribute("userList",userList);
            return "userlist";
        } else {
            model.addAttribute("errorMessage", "空空如也");
            //session.setAttribute("errorMessage","空空如也");
            return "frame";
        }
    }

    //跳转至添加页面
    @RequestMapping("toUserAdd")
    public String toUserAdd(Model model) {
        List<Role> roleList = null;
        roleList = roleService.selectRoleList();
        model.addAttribute("roleList", roleList);
        return "useradd";
    }

    //保存添加信息
    public String saveUserAdd() {
        return "";
    }

    //点击查看用户详细信息
    @RequestMapping("view")
    public String getUserView(Model model, @RequestParam(value = "userid", required = false) Long id) {
        User user = userService.selectUserById(id);
        model.addAttribute("user", user);
        return "userview";
    }

    @RequestMapping("modifyUser")
    public String toModifyUser(Model model, @RequestParam(value = "userid", required = false) Long id) {
        //前端拿到的数据永远是String
        List<Role> roleList = null;
        roleList = roleService.selectRoleList();
        model.addAttribute("roleList", roleList);
        User user = userService.selectUserById(id);
        model.addAttribute("user", user);
        return "usermodify";
    }

    @RequestMapping("saveModifyUser")
    public String saveModifyUser(Model model, User user) {
        System.out.println(user.getUserName());
        int i = userService.modifyUserById(user);
        if (i > 0) {
            // model.addAttribute()
            return "userlist";
        } else {
            model.addAttribute("errorMessage", "添加失败");
            return "error";
        }

    }

    @RequestMapping("deleteUser")
    public String deleteUserById(@RequestParam(value = "userid", required = false) String id) {
        int i = userService.deleteUserById(id);
        if (i > 0) {
            return "userlist";
        } else {
            return "";
        }

    }

}
